package com.bella.api.system.service.impl;

import com.bella.api.configuration.BusinessException;
import com.bella.api.system.dao.mapper.ext.AuthMenuExtendMapper;
import com.bella.api.system.dao.mapper.ext.AuthRoleMenuExtendMapper;
import com.bella.api.system.dao.model.AuthMenu;
import com.bella.api.system.dto.menu.ChildMenuDto;
import com.bella.api.system.dto.menu.MenuDtoRes;
import com.bella.api.system.dto.menu.MenuQueryDto;
import com.bella.api.system.service.MenuService;
import com.bella.api.utils.AdminContext;
import com.bella.api.utils.ApiResponseCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zsq
 * @create 2020-05-14-16:05
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    AuthMenuExtendMapper authMenuExtendMapper;
    @Autowired
    AuthRoleMenuExtendMapper authRoleMenuExtendMapper;

    /**
     * 菜单列表
     *
     * @param menuQueryDto
     * @return
     */
    @Override
    public List<MenuDtoRes> getMenuList(MenuQueryDto menuQueryDto) {
        List<AuthMenu> menuList = authMenuExtendMapper.getMenuList();
        if (menuList.size() > 0) {
            return convert(menuList);
        } else {
            return new ArrayList<>();
        }
    }

    private List<MenuDtoRes> convert(List<AuthMenu> menuList) {
        List<AuthMenu> parentLevelMenuList = menuList.stream().filter(data -> data.getParentId() == 0).collect(Collectors.toList());

        List<MenuDtoRes> menuDtoResList = new ArrayList<>();
        List<ChildMenuDto> childMenuDtoResList = null;
        MenuDtoRes menuDtoRes = null;
        for (AuthMenu authMenu : parentLevelMenuList) {
            menuDtoRes = new MenuDtoRes();
            BeanUtils.copyProperties(authMenu, menuDtoRes);
            menuDtoRes.setParentName("系统管理");
            List<AuthMenu> childMenuList = menuList.stream().filter(data -> data.getParentId() == authMenu.getId()).collect(Collectors.toList());
            childMenuDtoResList = new ArrayList<>();
            for (AuthMenu menu : childMenuList) {
                ChildMenuDto child = new ChildMenuDto();
                BeanUtils.copyProperties(menu, child);
                child.setParentName(authMenu.getName());
                childMenuDtoResList.add(child);
            }
            menuDtoRes.setChildren(childMenuDtoResList);
            menuDtoResList.add(menuDtoRes);
        }
        return menuDtoResList;
    }

    /**
     * 菜单详情
     *
     * @param id
     * @return
     */
    @Override
    public AuthMenu menuDetail(Integer id) {
        return authMenuExtendMapper.selectByPrimaryKey(id);
    }

    /**
     * 菜单添加
     *
     * @param authMenu
     */
    @Override
    public void addMenu(AuthMenu authMenu) {
        authMenuExtendMapper.insertSelective(authMenu);
    }

    /**
     * 菜单修改
     *
     * @param authMenu
     */
    @Override
    public void updMenu(AuthMenu authMenu) {
        authMenuExtendMapper.updateByPrimaryKeySelective(authMenu);
    }

    /**
     * 菜单删除
     *
     * @param authMenu
     */
    @Override
    public void deleteMenu(AuthMenu authMenu) {
        List<AuthMenu> authMenuList = authMenuExtendMapper.getMenuByParentId(authMenu.getId());
        if (authMenuList.size() > 0) {
            throw new BusinessException(ApiResponseCode.parameter_is_missing.getCode(), "请先删除子菜单");
        } else {
            AuthMenu target = new AuthMenu();
            target.setId(authMenu.getId());
            target.setStatus(0);
            authMenuExtendMapper.updateByPrimaryKeySelective(target);
            authRoleMenuExtendMapper.deleteRoleMenuByMenuId(authMenu.getId());
        }
    }

    /**
     * 获取导航菜单
     *
     * @return
     */
    @Override
    public List<MenuDtoRes> nav() {
        List<AuthMenu> authMenuList = new ArrayList<>();
        if(AdminContext.get().getUserName().equals("admin")){
            authMenuList = authMenuExtendMapper.getMenuList();
        }else{
            authMenuList = authMenuExtendMapper.nav(AdminContext.get().getId());
        }
        if (authMenuList.size() > 0) {
            return convert(authMenuList);
        } else {
            return new ArrayList<>();
        }
    }
}
